package com.audaque.springboot.foshanupload.httpclient.model;


import com.audaque.springboot.foshanupload.core.result.R;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.Objects;

/**
 * @author zgb
 * @desc 封装HTTP状态码, 让http请求不抛异常
 * @date 2022-05-09 18:54:32
 */

@Slf4j
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ExtractRestTemplate {
    @Autowired
    private RestTemplate restTemplateForIpPort;

    /**
     *
     * @param url
     * @param request
     * @param responseType :直接传String.class就是json字符串
     * @param uriVariables
     * @param <T>
     * @return
     * @throws RestClientException
     */
    public <T> R<T> postForEntityWithNoException(String url, Object request, Class<T> responseType, Object... uriVariables) throws RestClientException {
        R<T> r = new R<T>();
        r.setSuccess(false);
        r.setCode("400");
        r.setData(null);
        ResponseEntity<T> tResponseEntity =null;
        try {
            tResponseEntity =restTemplateForIpPort.postForEntity(url, request, responseType, uriVariables);
        }catch (Exception e){
            e.printStackTrace();
            String msg=String.format("restTemplateForIpPort.postForEntity调用%s失败,异常信息：%s", url, e.getMessage());
            log.error(msg);
            r.setMsg(msg);
            return r;
        }

        if (tResponseEntity == null) {
            r.setMsg("tResponseEntity == null");
            return r;
        }
        log.debug("tResponseEntity：" + tResponseEntity);
        HttpStatus statusCode = tResponseEntity.getStatusCode();
        log.debug("statusCode：" + statusCode);
        int statusCodeValue = tResponseEntity.getStatusCodeValue();
        log.debug("statusCodeValue：" + statusCodeValue);
        T body = tResponseEntity.getBody();
        log.debug("body：" + body);
        HttpHeaders headers = tResponseEntity.getHeaders();
        log.debug("headers：" + headers);
        r.setData(body);
        r.setMsg(statusCode.name());
        r.setCode(Integer.toString(statusCodeValue));
        if (Objects.equals(statusCodeValue, 200)) {
            r.setSuccess(true);
        } else {
            r.setSuccess(false);
        }

        return r;
    }


    /**
     *
     * @param url
     * @param method
     * @param requestEntity
     * @param responseType ：直接传String.class就是json字符串
     * @param uriVariables
     * @param <T>
     * @return
     * @throws RestClientException
     */
    public <T> R<T> exchangeWithNoException(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) throws RestClientException {
        R<T> r = new R<T>();
        r.setSuccess(false);
        r.setCode("400");
        r.setData(null);
        ResponseEntity<T> tResponseEntity =null;
        try {
            tResponseEntity =restTemplateForIpPort.exchange(url, method, requestEntity, responseType, uriVariables);
        }catch (Exception e){
            e.printStackTrace();
            String msg=String.format("restTemplateForIpPort.exchange调用%s失败,异常信息：%s", url, e.getMessage());
            log.error(msg);
            r.setMsg(msg);
            return r;
        }

        log.debug("tResponseEntity：" + tResponseEntity);
        if (tResponseEntity == null) {
            r.setMsg("tResponseEntity == null");
            return r;
        }
        HttpStatus statusCode = tResponseEntity.getStatusCode();
        log.debug("statusCode：" + statusCode);
        int statusCodeValue = tResponseEntity.getStatusCodeValue();
        log.debug("statusCodeValue：" + statusCodeValue);
        T body = tResponseEntity.getBody();
        log.debug("body：" + body);
        HttpHeaders headers = tResponseEntity.getHeaders();
        log.debug("headers：" + headers);
        r.setData(body);
        r.setMsg(statusCode.name());
        r.setCode(Integer.toString(statusCodeValue));
        if (Objects.equals(statusCodeValue, 200)) {
            r.setSuccess(true);
        } else {
            r.setSuccess(false);
        }

        return r;
    }


}
